package com.example.computer_exam_training_sys.controller;

import com.example.computer_exam_training_sys.dto.AchievementDTO;
import com.example.computer_exam_training_sys.entity.UserAchievement;
import com.example.computer_exam_training_sys.service.UserAchievementService;
import com.example.computer_exam_training_sys.service.UserAchievementService.AchievementStatistics;
import com.example.computer_exam_training_sys.service.UserAchievementService.AchievementSuggestion;
import com.example.computer_exam_training_sys.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/achievements")
public class UserAchievementController {

    @Autowired
    private UserAchievementService achievementService;

    /**
     * 获取用户所有成就
     */
    @GetMapping("/user/{userId}")
    public Result<List<AchievementDTO>> getUserAchievements(@PathVariable Long userId) {
        List<AchievementDTO> achievements = achievementService.getUserAchievements(userId);
        return Result.success(achievements);
    }

    /**
     * 检查并解锁成就
     */
    @PostMapping("/check/{userId}")
    public ResponseEntity<Void> checkAchievements(
            @PathVariable Long userId,
            @RequestParam String activityType,
            @RequestParam Integer value) {
        achievementService.checkAchievements(userId, activityType, value);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取用户成就统计
     */
    @GetMapping("/statistics/{userId}")
    public ResponseEntity<AchievementStatistics> getAchievementStatistics(@PathVariable Long userId) {
        return ResponseEntity.ok(achievementService.getAchievementStatistics(userId));
    }

    /**
     * 获取用户成就建议
     */
    @GetMapping("/suggestions/{userId}")
    public ResponseEntity<List<AchievementSuggestion>> getAchievementSuggestions(@PathVariable Long userId) {
        return ResponseEntity.ok(achievementService.getAchievementSuggestions(userId));
    }
    
    /**
     * 从练习记录更新成就
     */
    @PostMapping("/update-from-practice/{userId}")
    public Result<String> updateFromPractice(@PathVariable Long userId) {
        achievementService.checkAchievements(userId, "practice", null);
        return Result.success("已从练习记录更新成就");
    }
    
    /**
     * 从考试记录更新成就
     */
    @PostMapping("/update-from-exam/{userId}")
    public Result<String> updateFromExam(@PathVariable Long userId) {
        achievementService.checkAchievements(userId, "exam", null);
        return Result.success("已从考试记录更新成就");
    }
    
    /**
     * 从学习进度更新成就
     */
    @PostMapping("/update-from-learning/{userId}")
    public Result<String> updateFromLearning(@PathVariable Long userId) {
        achievementService.checkAchievements(userId, "learning", null);
        return Result.success("已从学习进度更新成就");
    }
    
    /**
     * 从课程完成情况更新成就
     */
    @PostMapping("/update-from-course/{userId}")
    public Result<String> updateFromCourse(@PathVariable Long userId) {
        achievementService.checkAchievements(userId, "course", null);
        return Result.success("已从课程完成情况更新成就");
    }
    
    /**
     * 更新所有类型的成就
     */
    @PostMapping("/update-all/{userId}")
    public Result<String> updateAllAchievements(@PathVariable Long userId) {
        achievementService.checkAchievements(userId, "practice", null);
        achievementService.checkAchievements(userId, "exam", null);
        achievementService.checkAchievements(userId, "learning", null);
        achievementService.checkAchievements(userId, "course", null);
        return Result.success("已更新所有类型的成就");
    }
    
    /**
     * 获取用户最近解锁的成就
     */
    @GetMapping("/latest/{userId}")
    public Result<List<UserAchievement>> getLatestAchievements(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "5") Integer limit) {
        return Result.success(achievementService.getLatestAchievements(userId, limit));
    }
} 